<?php
/**
 * （logic）创建基础备货国家sku提货（EU备货国家）
 */
namespace App\Logic\SsBasicCountry;

use App\Models\Basics\SysArea;
use App\Models\Basics\SysICountry;
use App\Models\Basics\Transport;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\CountrySku;
use App\Models\SsBasic\CountrySkuAvg;
use App\Models\SsBasic\CountrySkuEstimate;
use App\Models\SsBasic\CountrySkuStock;
use App\Models\SsBasic\CountryStockingSku;
use App\Models\SsBasic\CountryStockingSkuRatio;
use Exception;
class initCountrySyncSS
{
    /**
     * 同步国家备货基础数据 （country:sync-ss）
     * 设定：每天运行
     */
    public static function exe()
    {

        $euAreaId = SysArea::AREA_EU; //EU站点
        //EU国家
        $euCountryIds = SysICountry::getCountryIdsByAreaId($euAreaId);
        if (!$euCountryIds) die('stop!');

        //亚马逊sku
        $productAmazonSku = ProductAmazonSku::query()
            ->where('status',ProductAmazonSku::STATUS_YES) //启用
            ->whereIn('country_id', $euCountryIds)
            ->select('country_id','psku_id')
            ->distinct()
            ->get();
        $new = [];
        foreach($productAmazonSku as $amazonSku){
            $value = $amazonSku->country_id.'_'.$amazonSku->psku_id;
            if(!in_array($value,$new)){
                $new[] = $value;
            }
        }
        $new = $new ? array_unique($new) : []; //数组去重
        $count = count($new);
        if($count < 2100){
            report(new Exception("Failed to artisan sync-ss : count(" . $count . ")总数异常",121));
            die(0);
        }

        //需要删除的数据(基础sku)
        $old = CountrySku::query()->pluck('area_psku')->toArray();
        $delete = array_diff($old,$new);
        foreach($delete as $_delete){
            list($countryId,$pSkuId) = explode('_',$_delete);
            $count = ProductAmazonSku::query()
                ->where('status',ProductAmazonSku::STATUS_YES)
                ->where('country_id',$countryId)
                ->where('psku_id',$pSkuId)
                ->count();
            if($count > 0)continue;
            CountrySku::query()->where('area_psku',$_delete)->delete(); //基础sku
            CountrySkuAvg::query()->where('area_psku',$_delete)->delete(); //sku均值
            CountrySkuStock::query()->where('area_psku',$_delete)->delete(); //sku库存
            CountrySkuEstimate::query()->where('area_psku',$_delete)->delete(); //sku预估
            CountryStockingSkuRatio::query()->where('area_psku',$_delete)->delete(); //sku销售比例
        }

        //需要新增的数据
        $add = array_diff($new,$old);
        $skuArr = [];
        $price = [];
        $pCycle = [];
        $transportArr = [];

        foreach($add as $_add){
            $count = CountrySku::query()->where('area_psku',$_add)->count();
            if($count > 0)continue;
            list($countryId,$pSkuId) = explode('_',$_add);
            if(!array_key_exists($pSkuId,$skuArr)){
                $skuArr[$pSkuId] = ProductSku::getModelById($pSkuId);
            }
            $sku = $skuArr[$pSkuId];

            if(!array_key_exists($pSkuId,$price)){
                $price[$pSkuId] = ProductSku::getPuPrice($sku);
            }

            if(!array_key_exists($pSkuId,$pCycle)){
                $pCycle[$pSkuId] = ProductSku::getPuCycle($sku);
            }

            if(!array_key_exists($countryId,$transportArr)){
                $transportArr[$countryId] = Transport::getTransportDay($euAreaId); //EU站点运输日期
            }
            $transport = $transportArr[$countryId];

            //备货国家sku
            CountrySku::addCountrySku($countryId,$pSkuId,$transport->air_day ?? 0,$transport->sea_day ?? 0,$pCycle[$pSkuId],$price[$pSkuId]);
        }
    }
}
